package com.dgut.healthreportmanage.service;

import com.dgut.healthreportmanage.mapper.UserMapper;
import com.dgut.healthreportmanage.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;

    @Override
    public User getUserByUserNo(String userNo) {
        User user = userMapper.getUserByUserNo(userNo);
        if (user == null) {
            user = new User();
            user.setUserId(0);
        }
        return user;
    }

    @Override
    public List<User> getAllUsers() {
        return userMapper.getAllUsers();
    }

    @Override
    public int addUser(User user) {
        //新用户
        if (user.getUserId() == 0) {
            int result = userMapper.addUser(user);
            if (result == 0) {
                return 0;
            }
            User userByUserNo = userMapper.getUserByUserNo(user.getUserNo());
            if (userByUserNo == null) {
                return 0;
            }
            return userByUserNo.getUserId();
        }
        else {
            int result = userMapper.updateUser(user);
            if (result == 0) {
                return 0;
            }
            return user.getUserId();
        }
    }
}
